//
//  NewsDetailViewController.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/2.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "NetEaseDataService.h"
#import "NewsDetailModel.h"
#import "NewsDetailTableView.h"

@interface NewsDetailViewController ()

@end

@implementation NewsDetailViewController {
    UIButton* replyBtn;         //跟帖
    
    NewsDetailTableView* _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self _createTableView];   //添加表视图
    [self _createNavBar];       //顶部返回按钮和跟帖
    
    [self _loadData];           //网络数据请求
}

- (void)viewWillAppear:(BOOL)animated {
    //这个页面里面的statusBar是黑色的
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)_loadData {
    [NetEaseDataService newsDetailDataRequestNewsId:_newsId block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"新闻详情 页面数据获取失败 %@", result);
            return;
        }
//        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        if (result[_newsId]) {
            NewsDetailModel* model = [[NewsDetailModel alloc] initWithDataDic:result[_newsId]];
            _tableView.newsDetailModel = model;
            _replyId = model.replyId;
            
            //刷新跟帖数量的显示
            NSInteger replyCount = [model.replyCount integerValue];
            [replyBtn setTitle:[NSString stringWithFormat:@"%li跟帖", replyCount] forState:UIControlStateNormal];
        }
    }];
}

#pragma mark - 子视图们
- (void)_createNavBar {
    //返回按钮
    UIButton* backBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 25, 48, 40)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    //跟帖
    replyBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth-80, 25, 90, 40)];
    [replyBtn setBackgroundImage:[UIImage imageNamed:@"contentview_commentbacky"] forState:UIControlStateNormal];
    replyBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    replyBtn.titleLabel.textColor = [UIColor whiteColor];
    replyBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [replyBtn setTitle:@"000跟帖" forState:UIControlStateNormal];
    [replyBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
    [replyBtn addTarget:self action:@selector(replyBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:replyBtn];
    
    //灰色分割线
    UIImageView* lineImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kWidth, 0.25)];
    UIImage* line = [UIImage imageNamed:@"comment_ground_1"];
    [line stretchableImageWithLeftCapWidth:50 topCapHeight:1];
    lineImgView.image = line;
    [self.view addSubview:lineImgView];
}

- (void)_createTableView {
    _tableView = [[NewsDetailTableView alloc] initWithFrame:CGRectMake(0, navBarHeight, kWidth, kHeight-navBarHeight)];
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
}


#pragma mark - actions
- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)replyBtnAction {
    //跟帖抓包失败
//    [NetEaseDataService replyDetailRequestReplyId:_replyId replyType:@"hot" block:^(id result) {
//        if ([result isKindOfClass:[NSError class]]) {
//            NSLog(@"跟帖详情数据 请求失败 %@", result);
//            return;
//        }
//        NSLog(@"%@ %@", _replyId, [Tools getJsonStrFromObj:result]);
//        
//    }];
}


@end
